import 'package:base_flutter/base_flutter.dart';
import 'package:fan_da_app/model/user_model.dart';
import 'package:fan_da_app/ui/home_main_page.dart';

class LoginViewModel extends BaseViewModel{
  LoginViewModel(super.context);

  var username = "";
  var password = "";

  ///0登录，1注册
  var currentStep = 0;

  var model = UserModel();

  void registerClick(){
    currentStep = 1;
    notifyListeners();
  }

  void toLoginClick(){
    currentStep = 0;
    notifyListeners();
  }

  Future<void> login() async {
    if(username.isNullOrEmpty()){
      showToast("请输入账号");
      return;
    }
    if(password.isNullOrEmpty()){
      showToast("请输入密码");
      return;
    }

    showLoadingDialog(msg: "登录中...");
    try{
      var result = await model.login(username: username, password: password);
      hideDialog();
      Go().pushAndPop(HomeMainPage());
    }catch(e){
      showToast(e.toNetError());
      hideDialog();
    }
  }
}